
#include "gd32f30x.h"
#include "systick.h"
#include <stdio.h>
#include "timer.h"

//定时器配置
void timer_config(void)
{
    /* -----------------------------------------------------------------------
	系统主频108MHZ,timer_initpara.prescaler为1079，timer_initpara.period为9999，
    频率就为10HZ，100ms进一次中断
    ----------------------------------------------------------------------- */
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(RCU_TIMER1);
    timer_deinit(TIMER1);
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 1079;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 9999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);
	nvic_irq_enable(TIMER1_IRQn, 0, 1);
	timer_interrupt_enable(TIMER1, TIMER_INT_UP);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER1);
    /* auto-reload preload enable */
    timer_enable(TIMER1);
}

#if ENABLE_PWM_TEST == 1

static void pwm_channel_gpio_config(void)
{
    // rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_AF);

    /*Configure PA6 PA7 PB0 PB1(TIMER2 CH0 CH1 CH2) as alternate function*/
    // gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
    // gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);

    gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
    // gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
}

static void timer2_config(void)
{
    /* -----------------------------------------------------------------------
    TIMER2 configuration: generate 3 PWM signals with 3 different duty cycles:
    TIMER2CLK = SystemCoreClock / 108 = 1MHz
    108M/(107+1)/(999+1) = 1000HZ
    TIMER2 channel0 duty cycle = (250/ 1000)* 100  = 25%
    TIMER2 channel1 duty cycle = (500/ 1000)* 100  = 50%
    TIMER2 channel2 duty cycle = (750/ 1000)* 100 = 75%
    ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER2);

    timer_deinit(TIMER2);

    /* TIMER2 configuration */
    timer_initpara.prescaler         = 107;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 499;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER2,&timer_initpara);

    /* CH0,CH1 and CH2 configuration in PWM mode */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    // timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);
    // timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);
    timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);

    // /* CH0 configuration in PWM mode0,duty cycle 25% */
    // timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,249);
    // timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    // timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    // /* CH1 configuration in PWM mode0,duty cycle 50% */
    // timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,499);
    // timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    // timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* CH2 configuration in PWM mode0,duty cycle 75% */
    timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,349); //设置占空比
    timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

	// nvic_irq_enable(TIMER2_IRQn, 0, 2);
	// timer_interrupt_enable(TIMER2, TIMER_INT_UP);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER2);
    /* auto-reload preload enable */
    timer_disable(TIMER2);
}
// 使用通用定时器TIM2 产生PWM
void pwm_config(void)
{
  pwm_channel_gpio_config();
  timer2_config();
}


#endif //if ENABLE_PWM_TEST == 1

//定时器配置
void timer3_config(void)
{
    /* -----------------------------------------------------------------------
	系统主频108MHZ,timer_initpara.prescaler为107，timer_initpara.period为65535，
    一us加一个数，65535重新计数，最大延时us为65535-1
    ----------------------------------------------------------------------- */
    timer_parameter_struct timer_initpara;
    rcu_periph_clock_enable(RCU_TIMER3);
    timer_deinit(TIMER3);
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 108-1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 65535-1;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER3,&timer_initpara);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER3);
    /* auto-reload preload enable */
    timer_disable(TIMER3);
}

void tim3_delay_us(__IO uint32_t us)
{
    timer_counter_value_config(TIMER3,0);
    timer_enable(TIMER3);

    while(timer_counter_read(TIMER3)< us);
    timer_disable(TIMER3);
}


